/*******************************************************************************
 * Copyright (c) 2014 Greg Marut.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 * 
 * Contributors:
 *    Greg Marut - initial API and implementation
 ******************************************************************************/
package com.gregmarut.serialization;

import java.io.IOException;
import java.io.Serializable;

import com.google.gson.Gson;

public class GsonSerializer implements Serializer
{
	private final Gson gson;
	
	public GsonSerializer()
	{
		gson = new Gson();
	}
	
	@Override
	public byte[] flattenObject(final Serializable serializable) throws IOException
	{
		return gson.toJson(serializable).getBytes();
	}

	@Override
	public <T extends Serializable> T restoreObject(byte[] bytes, Class<T> clazz) throws SerializationException
	{
		return gson.fromJson(new String(bytes), clazz);
	}
}
